<?php

require_once("XMLParser.php");

/**
 * This class retreives, parses and displays information from the Guitar Hero Community website ( http://community.guitarhero.com )
 * 
 * @package guitar-hero-stats
 * @version 1.0
 * 
 * @license The MIT License
 * 
 * Copyright (c) 2009 Beau Durrant
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * */

class GuitarHeroStats  {
	
	/** @var string xml feed prefix, append <game>/<account>.xml */
	private $feedPrefix = 'http://community.guitarhero.com/accounts/feed/';
	/** @var array allowed games and number of songs */
	private $games = array('gh3' => 74, 'aerosmith' => 41, 'ghwt' => 86, 'ghmetallica' => 52, 'ghsmashhits' => 48);
	/** @var array allowed difficulties */
	private $difficulties = array('easy', 'medium', 'hard', 'expert');
	/** @var string xml returned from the API */
	private $xml;
	/** @var array xml parsed into an array */
	private $xmlValues = array();
	/** @var int account id */
	private $account;
	/** @var string game difficulty */
	private $difficulty;
	/** @var string GH game */
	private $game;
	/** @var hex background color */
	private $backgroundColor = 0xFFFFFF;
	/** @var hex text color */
	private $textColor = 0x000000;
	/** @var string font file */
	private $font = 'C:\windows\fonts\arial.ttf';

	/**
	 * When created a call is made to the appropriate XML feed
	 * The data is then parsed and stored in a key => value array for quick reference
	 * 
	 * @param int $account Account id to retreive the xml for from community.guitarhero.com
	 * @param string $difficulty Easy, medium, hard or expert
	 * @param string $game Which GH game to get the xml feed for
	 * @return void
	 * */
	public function GuitarHeroStats($account, $difficulty, $game){
		
		// make sure we will have everything to complete the request for the feed
		if( !is_numeric( $account ) ) $account = 422157;
		if( !in_array( $difficulty, $this->difficulties ) ) $difficulty = "expert";
		if( !array_key_exists( $game, $this->games ) ) $game =  "ghwt";
			
		// set the values
		$this->account = $account;
		$this->difficulty = $difficulty;
		$this->game = $game;

		// load and parse the xml
		$this->xml = file_get_contents('http://community.guitarhero.com/accounts/feed/'.$game.'/'.$account.'.xml');
		$p = new XMLParser($this->xml);
		$this->xmlValues = $p->getOutput();
		$this->songs = $this->xmlValues['account']['primary_slot']['highscores']['regular'][$this->difficulty];
		
	}
	
	/**
	 * Calculates the amount of stars gained vs total stars
	 * Displays an image when called
	 * 
	 * @return void
	 * */
	public function displayStarsSignatureImage(){
		
		// total and max stars
		$totalStars = 0;
		$maxStars = 0;
		
		// loop through the array to get stars for all songs
		if(count($this->songs) > 0){
			foreach($this->songs as $song){
				$totalStars += $song['stars'];
				$maxStars += 5;
			}
		}
		
		// create the image standard for signatures
		$image = $this->createSignatureImage();
		
		// place the star info over the image
		imagettftext($image, 8, 0,  110, 31, $this->textColor, $this->font, "Total Stars: ");
		imagettftext($image, 8, 0,  180, 31, $this->textColor, $this->font, $totalStars);
		imagettftext($image, 8, 0,  110, 45, $this->textColor, $this->font, "Missed Stars: ");
		imagettftext($image, 8, 0,  180, 45, $this->textColor, $this->font, $maxStars - $totalStars);
		
		// display the image
		$this->displayImage($image);
		
	}
	
	/**
	 * Calculates the number of cleared, five stared and full combos
	 * Displays an image when called
	 * 
	 * @return void
	 * */
	public function displayCompletedSignatureImage(){
		
		// five stars, fc and completed
		$fiveStars = 0;
		$fullCombos = 0;
		$completed = 0;
		
		// loop through the array to get stars and match them to completed, fc or five stars
		if(count($this->songs) > 0){
			foreach($this->songs as $song){
				if($song['stars'] == 5) $fiveStars++;
				if($song['hit_percentage'] == 100) $fullCombos++;
				if($song['stars'] >= 3) $completed++;
			}
		}
		
		// create the image standard for signatures
		$image = $this->createSignatureImage();
		
		// place the completed song information over the image
		imagettftext($image, 8, 0, 110, 24, $this->textColor, $this->font, "Cleared: ");
		imagettftext($image, 8, 0, 180, 24, $this->textColor, $this->font,  $completed);
		
		imagettftext($image, 8, 0, 110, 35, $this->textColor, $this->font, "Five Stars: ");
		imagettftext($image, 8, 0, 180, 35, $this->textColor, $this->font, $fiveStars);
		
		imagettftext($image, 8, 0, 110, 46, $this->textColor, $this->font, "Full Combos: ");
		imagettftext($image, 8, 0, 180, 46, $this->textColor, $this->font, $fullCombos);
		
		// display the image
		$this->displayImage($image);
		
	}
	
	/**
	 * Creates the template for the signatures 
	 * Includes game logo, username and platform logo 
	 * 
	 * @return string $game Which GH game to get the xml feed for
	 * */
	private function createSignatureImage()
	{
	
		// create the image with the background and 200x50 to fit in signatures on community.guitarhero.com
		$image = imagecreatetruecolor(200, 50);
		imagefilledrectangle($image, 0, 0, 200, 50, $this->backgroundColor);
		
		// add difficulty
		imagettftext($image, 10, 0, 110, 13, $this->textColor, $this->font, ucwords($this->difficulty));
		
		// add the logo based on the game
		$logo = imagecreatefromjpeg($this->game . '.jpg');
		imagecopymerge($image, $logo, 0, 0, 0, 0, 58, 35, 100);
		
		// add platform logo
		$platform = $this->xmlValues['account']['primary_slot']['platform'];
		if( $platform )
		{
			// wii, xbox, ps3, 
			$platformLogo = imagecreatefromjpeg($platform.'.jpg');
			imagecopymerge($image, $platformLogo, 58, 0, 0, 0, 40, 35, 100);			
		}
		
		// add name
		imagettftext($image, 10, 0, 5, 46, $this->textColor, $this->font, ($this->xmlValues['account']['username']));
		
		// return the image
		return $image;
	
	}
	
	/**
	 * Sets the header type and displays an image
	 * 
	 * @param image $image Image to be displayed
	 * @return void
	 * */
	private function displayImage($image)
	{
		
		// Set the content type header to image/jpeg
		header("Content-type: image/png");	
		
		// Output the image
		imagepng($image);
		
		// Free up memory
		imagedestroy($image);	
		
	}
	
}

?>
